/*
 * @Author: yusha
 * @Date: 2023-06-13 15:33:41
 * @LastEditors: yusha
 * @LastEditTime: 2023-07-11 17:06:35
 * @Description: 接口
 */
import axios from 'axios';
import { message } from 'antd';
import { serviceConfig } from './swaggerServiceConfig';

/** 接口前缀 */
const newConfig = { ...serviceConfig };
/** 获取token */
const getUserInLocal = () => {
    const token = window.localStorage.getItem('token');
    if (!token) return;
    return token;
};
const axiosOemPurchase = axios.create({
    baseURL: 'https://gateway-prod.theckb.com',
    headers: { 
        'X-Authtoken': getUserInLocal() ,
        'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
    }
});

// 请求拦截
axiosOemPurchase.interceptors.request.use(function (config) {
    config.headers = config.headers ?? {};
    const isPost = ['POST', 'post'].includes(config.method!);
    const token = getUserInLocal();
    if (token) {
        config.headers['token'] = token;
    } else {
        window.location.href = '/opsManager/login';
    }
    if (isPost) {
        if (!config.data && config.params) {
            config.data = config.params;
        }
        config.headers['Content-Type'] = 'application/json';
    }
    return config;
});

// 返回拦截
axiosOemPurchase.interceptors.response.use(function (response) {
    if (response.data.code === 200 || response.data.code === '0' || response.data.code == 0 ) return response;

    if (['20001', '20002', '24010061', '25000001', '25000002'].includes(response.data.code)) {

        message.error(response.data.msg);

        setTimeout(() => {
            window.location.href = '/opsManager/login';
        }, 1000);
        return response;
    }

    if(['26140001'].includes(response.data.code)){
      message.error(response.data.msg);
    }

    return Promise.reject(response.data.msg);


}, function (error) {
    message.error('请求错误：' + String(error));
    return Promise.reject(error);
});


// export const qryCustomerAllDetail = (params) => {
//     return axiosOemPurchase({ url: '/quickQuery/customer/qryCustomerAllDetail', method: 'GET', params });
// };

// export const qryPlatformOrderStatus = (params) => {
//     return axiosOemPurchase({ url: '/quickQuery/order/qryPlatformOrderStatus', method: 'GET', params});
// };

// export const qryPurchaseDetail = (params) => {
//     return axiosOemPurchase({ url: '/quickQuery/purchase/qryPurchaseDetail', method: 'GET', params });
// };

// export const qryPurchaseNoRelation = (params) => {
//     return axiosOemPurchase({ url: '/quickQuery/purchase/qryPurchaseNoRelation', method: 'GET', params });
// };

// export const qryFlowByAbnormalNo = (params) => {
//     return axiosOemPurchase({ url: '/quickQuery/settlement/qryFlowByAbnormalNo', method: 'GET', params });
// };

// export const qryDeliveryTask = (params) => {
//     return axiosOemPurchase({ url: '/quickQuery/wms/qryDeliveryTask', method: 'GET', params });
// };

export const qryPkgInfo = (params) => {
    return axiosOemPurchase({ url: '/wms/ops/getPkgInfo', method: 'GET', params });
};

export const qryCustomerStockPkg = (params) => {
  return axiosOemPurchase({ url: '/wms/ops/queryCustomerStockPkg', method: 'POST', params });
};

export const adjustPkgInventory = (params) => {
  return axiosOemPurchase({ url: '/wms/ops/adjustPkgInventory', method: 'POST', params });
};

export const qryPkgAdjustRecord = (params) => {
    return axiosOemPurchase({ url: '/wms/ops/getPkgAdjustRecord', method: 'GET', params });
};

export const usersList = (params) => {
  return axiosOemPurchase({ url: '/api/sniff/users/list', method: 'POST', params });
};